package com.ml.mall.service.sys.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ml.mall.bean.req.sys.SysPermissionDto;
import com.ml.mall.bean.req.sys.SysPermissionEditDto;
import com.ml.mall.bean.req.sys.SysPermissionNewDto;
import com.ml.mall.consts.CommonConsts;
import com.ml.mall.entity.sys.SysPermission;
import com.ml.mall.mapper.sys.SysPermissionMapper;
import com.ml.mall.service.sys.SysPermissionService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
* <p>
    * 菜单权限表 服务实现类
    * </p>
*
* @author kyyang
* Created 2021-01-20
*/
@Service
public class SysPermissionServiceImpl extends ServiceImpl<SysPermissionMapper, SysPermission> implements SysPermissionService {

    @Autowired
    private  SysPermissionMapper  sysPermissionMapper;


    @Override
    @Transactional
    public int sysPermissionDel(List<Long> ids) {
        return sysPermissionMapper.deleteBatchIds(ids);
    }

    @Override
    public IPage<SysPermission> sysPermissionByPage(SysPermissionDto sysPermissionDto) {
        IPage<SysPermission> page = new Page<>(sysPermissionDto.getPageIndex(),sysPermissionDto.getPageNum());
        LambdaQueryWrapper<SysPermission> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysPermission::getType, CommonConsts.PERMISSION_TYPE_OF_MENU);
        return sysPermissionMapper.selectPage(page, queryWrapper);
    }

    @Override
    @Transactional
    public int sysPermissionNew(SysPermissionNewDto sysPermissionNewDto) {
        SysPermission sysPermission = new SysPermission();
        BeanUtils.copyProperties(sysPermissionNewDto,sysPermission);
        return sysPermissionMapper.insert(sysPermission);
    }

    @Override
    @Transactional
    public int sysPermissionEdit(SysPermissionEditDto sysPermissionEditDto) {
        SysPermission sysPermission = new SysPermission();
        BeanUtils.copyProperties(sysPermissionEditDto,sysPermission);
        return sysPermissionMapper.updateById(sysPermission);
    }

    @Override
    public List<Integer> querySubPermissionIds(List<Integer> menuIds) {
        LambdaQueryWrapper<SysPermission> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(SysPermission::getParentId, menuIds)
                    .eq(SysPermission::getType, CommonConsts.PERMISSION_TYPE_OF_PERMISSION);
        List<SysPermission> sysPermissions = sysPermissionMapper.selectList(queryWrapper);
        return sysPermissions.stream().map(SysPermission::getId).collect(Collectors.toList());
    }
}
